package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.SaleInfoBean;
import com.lovo.invoicing.bean.vo.SaleGoodsBean;
import com.lovo.invoicing.bean.vo.SaleInfoSaleBean;
import com.lovo.invoicing.bean.vo.SaleMonthBean;
import com.lovo.invoicing.bean.vo.SaleTypeBean;
import com.lovo.invoicing.mapper.SaleInfoMapper;
import com.lovo.invoicing.service.ISaleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SaleInfoServiceImpl extends ServiceImpl<SaleInfoMapper, SaleInfoBean> implements ISaleInfoService {
   @Autowired
   SaleInfoMapper saleInfoMapper;
    @Override
    public IPage<SaleInfoBean> querySaleInfoByPage(PageBean<SaleInfoBean> pageBean) {
        QueryWrapper<SaleInfoBean> qw = new QueryWrapper<>();
        if (pageBean.getT().getClientName()!=null && !"".equals(pageBean.getT().getClientName())){
            qw.like("client_name",pageBean.getT().getClientName());
        }
        if (pageBean.getStartDate()!=null && !"".equals(pageBean.getStartDate())){
            qw.ge("sale_date",pageBean.getStartDate());
        }
        if (pageBean.getEndDate()!=null && !"".equals(pageBean.getEndDate())){
            qw.le("sale_date",pageBean.getEndDate());
        }
        return baseMapper.selectPage(new Page<>(pageBean.getPageNo(), pageBean.getPageSize()),qw);
    }

    @Override
    public List<SaleInfoSaleBean> queryBySaleInfoId(int saleInfoId) {
        return saleInfoMapper.selectBySaleInfoId(saleInfoId);
    }

    @Override
    public List<SaleTypeBean> queryTypeTotal(String startDate, String endDate) {
        return saleInfoMapper.selectTypeTotal(startDate,endDate);
    }

    @Override
    public List<SaleGoodsBean> queryNameTotal(String startDate, String endDate) {
        return saleInfoMapper.selectNameTotal(startDate,endDate);
    }


    @Override
    public int addSaleInfo(SaleInfoBean saleInfoBean) {
        baseMapper.insert(saleInfoBean);
        return saleInfoBean.getSaleinfoId();
    }

    @Override
    public List<String> queryAllSaleYears() {
        return saleInfoMapper.selectAllSaleYear();
    }

    @Override
    public List<Double> queryComputerPrices(String saleYear) {
        return saleInfoMapper.selectComputerPrices(saleYear);
    }

    @Override
    public List<Double> querySecurityPrices(String saleYear) {
        return saleInfoMapper.selectSecurityPrices(saleYear);
    }

    @Override
    public List<Double> queryWorkPrices(String saleYear) {
        return saleInfoMapper.selectWorkPrices(saleYear);
    }

    @Override
    public List<String> queryMonthsBySaleYear(String saleYear) {
        return saleInfoMapper.selectMonthsByYear(saleYear);
    }
}
